/*
 *MIT License
 *
 *Copyright (c) 2019 chenshuai cs4380@163.com
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in all
 *copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 */

package com.cs.cslc.admin.biz;

import com.cs.cslc.admin.dto.MenuButtonCodeDTO;
import com.cs.cslc.admin.mapper.SysMenuButtonMapper;
import com.cs.cslc.admin.pojo.SysMenuButton;
import com.cs.cslc.common.biz.BaseBusinessBiz;
import com.cs.cslc.common.constant.CommonConstant;
import com.cs.cslc.common.constant.HttpStatusConstant;
import com.cs.cslc.common.exception.BusinessException;
import com.cs.cslc.common.msg.TableResultResponse;
import com.cs.cslc.common.pojo.ParamQuery;
import com.cs.cslc.common.util.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;

/**
 * SysMenuButton 系统基础菜单按钮表.
 *
 * @author cs
 * @version 1.0
 * @date 2019-11-12 10:22
 * @description
 */
@Service
public class SysMenuButtonBiz extends BaseBusinessBiz<SysMenuButtonMapper, SysMenuButton> {

    @Override
    public void insertSelective(SysMenuButton entity) {
        int count = this.getCountByButtonCode(entity.getButtonCode());
        if (count > 0) {
            throw new BusinessException("按钮编码已存在，请重新输入！");
        }
        super.insertSelective(entity);
    }

    /**
     * 通过按钮编码统计个数
     *
     * @param buttonCode 菜单编码
     * @return
     */
    public int getCountByButtonCode(String buttonCode) {
        if (StringUtils.isBlank(buttonCode)) {
            return 0;
        }
        SysMenuButton sysMenuButton = new SysMenuButton();
        sysMenuButton.setButtonCode(buttonCode);
        return super.selectCount(sysMenuButton);
    }

    @Override
    public void deleteById(Object id) {
        if (null == id) {
            throw new BusinessException("请选择待删除对象！", HttpStatusConstant.FAIL);
        }
        SysMenuButton sysMenuButton = new SysMenuButton();
        sysMenuButton.setId(Integer.valueOf(id.toString()));
        sysMenuButton.setButtonCode(sysMenuButton.getId() + "_" + sysMenuButton.getButtonCode());
        sysMenuButton.setIsDeleted(CommonConstant.IS_DELETED);
        super.updateSelectiveById(sysMenuButton);
    }

    @Override
    public TableResultResponse<SysMenuButton> selectTableByParamQuery(ParamQuery query) {
        String[] fields = new String[]{"id", "buttonTitle", "buttonCode", "url", "method", "description"};
        return this.selectTableByParamQuery(query, fields);
    }

    /**
     * 通过主键id获取菜单按钮关系列表
     *
     * @param menuButtonIds 主键ids
     * @return
     */
    public List<MenuButtonCodeDTO> getMenuCodeAndButCodeByButIds(List<Integer> menuButtonIds) {
        if (BeanUtil.isEmpty(menuButtonIds)) {
            return new ArrayList<>();
        }
        List<MenuButtonCodeDTO> menuButtonList = this.baseMapper.selectMenuCodeAndButCodeByButIds(menuButtonIds);
        return BeanUtil.isEmpty(menuButtonList) ? new ArrayList<>() : menuButtonList;
    }

    /**
     * 获取所有菜单按钮关系
     *
     * @return
     */
    public List<MenuButtonCodeDTO> getAllMenuCodeAndButCode() {
        List<MenuButtonCodeDTO> menuButtonList = this.baseMapper.selectMenuCodeAndButCodeByButIds(null);
        return BeanUtil.isEmpty(menuButtonList) ? new ArrayList<>() : menuButtonList;
    }

    /**
     * 通过菜单按钮主键ids获取列表
     *
     * @param buttonIds 菜单按钮ids
     * @return "id", "buttonTitle", "buttonCode", "url", "method"
     */
    public List<SysMenuButton> getMenuButtonByIds(List<Integer> buttonIds) {
        String[] fields = new String[]{"id", "buttonTitle", "buttonCode", "url", "method"};
        Example example = new Example(SysMenuButton.class).selectProperties(fields);
        Criteria criteria = example.createCriteria();
        criteria.andIn("id", buttonIds);
        criteria.andEqualTo("isDeleted", CommonConstant.IS_UNDELETED);
        return this.baseMapper.selectByExample(example);
    }
}